package logic

import (
	"context"
	"strconv"
	"zk/product_zy17/pkg"
	"zk/product_zy17/productRpc/productRpc"

	"zk/product_zy17/api/internal/svc"
	"zk/product_zy17/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
	return &LoginUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginUserLogic) LoginUser(req *types.LoginUserRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	login, err := l.svcCtx.ProRpc.LoginUser(l.ctx, &productRpc.LoginUserRequest{
		Tel:     req.Tel,
		SmsCode: req.SmsCode,
	})

	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}

	handler, err := pkg.TokenHandler(strconv.FormatInt(login.Id, 10))

	return &types.Response{
		Message: "登陆成功",
		Code:    200,
		Data:    handler,
	}, nil
}
